package com.pandacc.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jolbox.bonecp.BoneCPDataSource;
import com.pandacc.dao.fiction.rowmapper.FictionChaptersDetailsBeanRowMapper;
import com.pandacc.pojo.fiction.FictionChaptersDetailsBean;
import com.pandacc.pojo.json.demo.Contact;

@Controller
public class GreetingController {

	@RequestMapping("/greet/{id}")
	public void greet(HttpServletRequest req, HttpServletResponse res, @PathVariable("id")long id) {
		String in = String.format("Current time is : %s, %s.",
				System.currentTimeMillis(),Long.valueOf(id));
		System.out.println(in);
		Map<String, Object> map2 = new HashMap<String, Object>(10);
		
        map2.put("a", "11");  
        map2.put("b", "22");  
        map2.put("c", "33");
        map2.put("user", id);
        ObjectMapper mapper = new ObjectMapper();
//        String jsonResult = JSON.toJSONString(datas);
		PrintWriter pw;
		try {
			String out = mapper.writeValueAsString(map2);
			pw = res.getWriter();
			pw.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/hello")
	public String hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
			Model model) {
		System.out.println(String.format("The input name is : %s", name));
		model.addAttribute("name", name);
		return "helloworld";
	}
	
	@RequestMapping(value="/index",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseStatus(HttpStatus.OK)
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res) {
		return new ModelAndView("index");
	}
	
	@RequestMapping(value="/welcome",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseStatus(HttpStatus.OK)
	public ModelAndView welcome(HttpServletRequest req, HttpServletResponse res) {
		return new ModelAndView("welcome");
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/contcts/all")
	public void getAllContacts(HttpServletRequest req, HttpServletResponse res){
		String jsonFile = "C:/Users/IBM_ADMIN/git/pandaCC/pandaCC/src/main/resources/data.json";
//		InputStream is = System.class.getResourceAsStream("data.json");
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		List<Contact> contactList;
		try {
			contactList = mapper.readValue(new File(jsonFile), List.class);
			String contactsStr = mapper.writeValueAsString(contactList);
			System.out.println(contactsStr);
			mapper.writeValue(res.getWriter(), contactList);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		GreetingController controller = new GreetingController();
		System.out.println(controller.getClass().getSimpleName());
	}
}
